package innatour;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class search extends innatour.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void mainsearch() throws Exception {
	  driver.manage().window().maximize();
	  driver.get(baseUrl);
	  for (int second = 0;; second++) {
		  if (second >= 180) fail("timeout");
		  try { if ("Москва, Россия".equals(driver.findElement(By.xpath("//input[@type='text']")).getAttribute("value"))) break; } catch (Exception e) {}
	    	Thread.sleep(1000);
	  }
	  driver.findElement(By.xpath("(//input[@type='text'])[2]")).clear();
	  driver.findElement(By.xpath("(//input[@type='text'])[2]")).sendKeys("ber");
	  driver.findElement(By.cssSelector("span.search-autocomplete-city.ng-binding")).click();
	  driver.findElement(By.xpath("//div[text()=\"Туда\"]")).click();
	  WebElement nextmonth = driver.findElement(By.cssSelector("td.calendar.calendar-1 > table.datepickerViewDays > thead > tr > th.datepickerGoNext > a"));
	  nextmonth.click();
	  nextmonth.click();
	  nextmonth.click();
	  nextmonth.click();
	  nextmonth.click();
	  nextmonth.click();
	  driver.findElement(By.cssSelector("table.datepickerViewDays > tbody.datepickerDays")).findElement(By.linkText("10")).click();
	  driver.findElement(By.cssSelector("table.datepickerViewDays > tbody.datepickerDays")).findElement(By.linkText("11")).click();
	  driver.findElement(By.cssSelector("div.search-form-item-current.ng-binding")).click();
	  driver.findElement(By.xpath("//div[2]/div/ul/li")).click();
	  driver.findElement(By.cssSelector("i.icon-sprite-search.search-button__icon")).click();
  
  }
  
  
 
  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
